package com.wch.clientdemo;

import com.google.gson.Gson;
import com.wch.clientdemo.module.base.network.VolleyOptions;
import com.wch.clientdemo.module.base.network.VolleyServe;
import com.wch.clientdemo.module.base.network.VolleyServe.Fail;
import com.wch.clientdemo.module.base.network.VolleyServe.Success;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private TextView tv;
	private Button btn;
	private Activity mActivity;
	private VolleyServe volleyServe;
	private VolleyOptions volleyOptions;
	private Handler mHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);    //这个是不要顶部丑陋的那些东西
		setContentView(R.layout.activity_main);
		mActivity = this;
		initData();
		initView();
		initEvent();
		initNet();
	}

	private void initNet() {
		// TODO Auto-generated method stub
		volleyServe = new VolleyServe(mActivity);
		volleyOptions = new VolleyOptions();
		configNet(volleyServe, volleyOptions);
	}

	private void initEvent() {
		// TODO Auto-generated method stub
		btn.setOnClickListener(this);
		
	}

	private void initView() {
		tv = (TextView) findViewById(R.id.text1);
		tv.setText("This is a test！！！");
		btn = (Button) findViewById(R.id.btn1);
	}

	private void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.equals(btn)){            //点击数据交互时的处理
			loadData(volleyOptions);
		}
	}
	
	/**
	 * 获取数据
	 * @param volleyOptions
	 */
	private void loadData(final VolleyOptions volleyOptions) {
		// TODO Auto-generated method stub
	/*	mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				getData(volleyOptions);
			}
		}, 1000);  */
		
		getData(volleyOptions);
		
	}

	protected void getData(VolleyOptions volleyOptions2) {
		// TODO Auto-generated method stub
		volleyServe.post(volleyOptions2, new Success() {

			@Override
			public void successCallback(Object object, int state) {
				// TODO Auto-generated method stub
			//	Toast.makeText(mActivity, object.toString(), Toast.LENGTH_LONG);
				tv.setText(object.toString());
				
			}
			
		}, new Fail() {
			
			@Override
			public void failCallback(String err) {
				// TODO Auto-generated method stub
			//	Toast.makeText(mActivity, err, Toast.LENGTH_LONG);
				tv.setText(err);
			}
		});
		
	}

	private void configNet(VolleyServe volleyServe,VolleyOptions volleyOptions){
		
	}
}
